/*
  The MIT License
  
  Copyright (c) 2009 smath project
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
  
  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
*/
package pl.smath.expression.model;

import java.util.List;
import java.util.Vector;

import pl.smath.expression.utils.SortProductComparator;


/**
 * Contains list of subterms like 2*3*x
 * 
 * @author Krzysztof Langner
 *
 */
public class ProductTerm extends AbstractMultiTerm{

	/**
	 * constructor
	 */
	public ProductTerm() {
		super("*");
	}


	/**
	 * Try to evaluate expression
	 */
	public Term evaluate(){

		float value = 1;

		for(Term term : getTerms()){
		
			Term eval = term.evaluate();
			if(eval instanceof NumberTerm){
				value *= ((NumberTerm)eval).getValue();
			}
			else{
				return this;
			}
		}
		
		if(isNegative()){
			value = -value;
		}
		
		return new NumberTerm(value);
	}
	

	/**
	 * Porówanie wyrazen
	 */
	public boolean isAbsTheSame(Term term){
		
		if(!(term instanceof ProductTerm))
			return false;
		
		return super.isAbsTheSame(term);
	}
	
	/**
	 * Uporzadkowanie minusow
	 */
	@Override
	public void normalizeSign(){
	
		boolean negative = false;
		for(Term term : getTerms()){
			if(term.isNegative()){
				negative = !negative;
				term.setNegative(false);
			}
		}
		
		setNegative(isNegative() != negative);

	}
	
	
	/**
	 * Usunięcie wewnetrznych nawiasów
	 * @return nowe wyrazenie lub to samo, jezeli nie ma wewnetrznych nawiasow
	 */
	@Override
	public Term removeInnerBrackets() {

		List<Term> terms = getTerms();
		List<Term> outputTerms = new Vector<Term>();
		Term outputTerm = this;
		boolean found = false;
		boolean negative = false;
		
		for(Term term : terms){
		
			if(term instanceof ProductTerm){
				
				found = true;
				negative = (negative != term.isNegative());
				for(Term insideTerm : ((ProductTerm)term).getTerms()){
					Term copyTerm = insideTerm.copy();
					copyTerm.setNegative(term.isNegative() != insideTerm.isNegative());
					outputTerms.add(copyTerm);
				}
			}
			else{
				outputTerms.add(term);
			}
		}
		
		
		if(found){
			outputTerm = createObject();
			outputTerm.setNegative(isNegative() != negative);
			((ProductTerm)outputTerm).addTerms(outputTerms);
			outputTerm.normalizeSign();
		}
		
		return outputTerm;
	}

	
	/**
	 * Proste sortowanie. Może wystarczy?
	 */
	@Override
	public void sort() {
		sortTerms(new SortProductComparator());
	}

	
	/**
	 * Zamiana do stringa
	 */
	public String toString(){
		String output = "";
		
		for(Term term : getTerms()){
			
			if(output.length() > 0){
				output += "*";
			}

			if(term instanceof SumTerm)
				output += "(" + term.toString() + ")";
			else
				output += term.toString();
		}
		
		if(isNegative())
			output = "-" + output;
		
		return output;
	}


	@Override
	protected AbstractMultiTerm createObject() {
		return new ProductTerm();
	}
	
}
